package v1

import (
	"blog/errmsg"
	"blog/middleware"
	"blog/model"
	"blog/utils"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Index(c *gin.Context) {
	c.HTML(http.StatusOK,"index.html",gin.H{"name":"ztind","age":18})
}

func Login(c *gin.Context){
	var user model.User
	if err := c.ShouldBindJSON(&user);err!=nil{
		utils.HttpResponse(c,http.StatusOK,errmsg.FAIL,"参数错误",nil)
	}else {
		u,code := user.Login()
		if code == errmsg.SUCCESS{
			//生成token
			u.Token,code = middleware.GetToken(u.Username,u.Password)
			if code == errmsg.SUCCESS {
				utils.HttpResponse(c,http.StatusOK,code,"登录成功",u)
			}else {
				utils.HttpResponse(c,http.StatusOK,code,u.Token,nil)
			}
		}else {
			utils.HttpResponse(c,http.StatusOK,code,"",u)
		}
	}
}